home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / NPP.PAK / NPVIEW.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  7KB  |  261 lines

  1. // npview.cpp : implementation of the CNotepadView class
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1995 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12. //
  13.  
  14. #include "stdafx.h"
  15. #include "np.h"
  16. #include "combobar.h"
  17. #include "mainfrm.h"
  18. #include "npdoc.h"
  19. #include "npview.h"
  20. #include "gotodlg.h"
  21. #include "finddlg.h"
  22.  
  23. #include <afxcoll.h>
  24.  
  25. #ifdef _DEBUG
  26. #undef THIS_FILE
  27. static char BASED_CODE THIS_FILE[] = __FILE__;
  28. #endif
  29.  
  30. /////////////////////////////////////////////////////////////////////////////
  31. // CNotepadView
  32.  
  33. IMPLEMENT_DYNCREATE(CNotepadView, CEditView)
  34.  
  35. BEGIN_MESSAGE_MAP(CNotepadView, CEditView)
  36.     //{{AFX_MSG_MAP(CNotepadView)
  37.     ON_COMMAND(ID_EDIT_GOTO, OnEditGoto)
  38.     ON_UPDATE_COMMAND_UI(ID_EDIT_GOTO, OnUpdateEditGoto)
  39.     ON_COMMAND(ID_EDIT_FIND, OnEditFind)
  40.     ON_UPDATE_COMMAND_UI(ID_EDIT_FIND, OnUpdateEditFind)
  41.     ON_COMMAND(ID_VIEW_FIND_NEXT, OnViewFindNext)
  42.     ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
  43.     ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
  44.     //}}AFX_MSG_MAP
  45.     // Standard printing commands
  46.     ON_COMMAND(ID_FILE_PRINT, CEditView::OnFilePrint)
  47.     ON_COMMAND(ID_FILE_PRINT_PREVIEW, CEditView::OnFilePrintPreview)
  48. END_MESSAGE_MAP()
  49.  
  50. /////////////////////////////////////////////////////////////////////////////
  51. // CNotepadView construction/destruction
  52.  
  53. CNotepadView::CNotepadView()
  54. {
  55.     m_pFindDialog = new CFindDlg;        
  56.     m_pGotoDialog = new CGotoDlg;
  57. }
  58.  
  59. CNotepadView::~CNotepadView()
  60. {
  61.     delete     m_pGotoDialog;
  62.     delete    m_pFindDialog;        
  63. }
  64.  
  65. /////////////////////////////////////////////////////////////////////////////
  66. // CNotepadView drawing
  67.  
  68. void CNotepadView::OnDraw(CDC* pDC)
  69. {
  70.     CNotepadDoc* pDoc = GetDocument();
  71.     ASSERT_VALID(pDoc);
  72. }
  73.  
  74. /////////////////////////////////////////////////////////////////////////////
  75. // CNotepadView printing
  76.  
  77. BOOL CNotepadView::OnPreparePrinting(CPrintInfo* pInfo)
  78. {
  79.     // default CEditView preparation
  80.     return CEditView::OnPreparePrinting(pInfo);
  81. }
  82.  
  83. void CNotepadView::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
  84. {
  85.     CEditView::OnBeginPrinting(pDC, pInfo);
  86. }
  87.  
  88. void CNotepadView::OnEndPrinting(CDC* pDC, CPrintInfo* pInfo)
  89. {
  90.     CEditView::OnEndPrinting(pDC, pInfo);
  91. }
  92.  
  93. /////////////////////////////////////////////////////////////////////////////
  94. // CNotepadView diagnostics
  95.  
  96. #ifdef _DEBUG
  97. void CNotepadView::AssertValid() const
  98. {
  99.     CEditView::AssertValid();
  100. }
  101.  
  102. void CNotepadView::Dump(CDumpContext& dc) const
  103. {
  104.     CEditView::Dump(dc);
  105. }
  106.  
  107. CNotepadDoc* CNotepadView::GetDocument() // non-debug version is inline
  108. {
  109.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CNotepadDoc)));
  110.     return (CNotepadDoc*)m_pDocument;
  111. }
  112. #endif //_DEBUG
  113.  
  114. /////////////////////////////////////////////////////////////////////////////
  115. // CNotepadView message handlers
  116.  
  117.  
  118. void CNotepadView::OnEditGoto() 
  119. {
  120.     if(m_pGotoDialog->DoModal() != IDOK)
  121.         return;
  122.  
  123.     if(m_pGotoDialog->m_nLineNum < 1)
  124.     {
  125.         CString s1;
  126.         s1.LoadString(NPERROR_INVALID_LINENUM);
  127.         AfxMessageBox(s1);
  128.         return;
  129.     }
  130.  
  131.     CEdit &edit = GetEditCtrl();
  132.     int i = edit.LineFromChar();            // this is the current line the cursor is on
  133.     int nLine = m_pGotoDialog->m_nLineNum;    // line number to go to
  134.  
  135.     if(nLine > (edit.GetLineCount()))
  136.     {
  137.         CString s1;
  138.         
  139.         s1.LoadString(NPERROR_NUM_TOO_BIG);
  140.         AfxMessageBox(s1);
  141.         return;
  142.     }
  143.  
  144.     // move window and caret
  145.     --nLine;                    // edit control is zero based
  146.     edit.LineScroll(nLine-i);    // new line number - the current line
  147.  
  148.     int idx;
  149.     idx = edit.LineIndex(nLine);
  150.     edit.SetSel(idx, idx);
  151. }
  152.  
  153. void CNotepadView::OnUpdateEditGoto(CCmdUI* pCmdUI) 
  154. {
  155.     pCmdUI->Enable(GetWindowTextLength());
  156. }
  157.  
  158. void CNotepadView::OnEditFind() 
  159. {
  160.     CString s1, s2;
  161.     CComboBox& comboBox = ((CMainFrame*)AfxGetMainWnd())->m_wndToolBar.m_toolBarCombo;
  162.  
  163.     // add user selected text from CEditView to the buffer
  164.     GetSelectedText(s1);
  165.     s2 = m_pFindDialog->m_szText; // save old string if user cancels
  166.  
  167.     if(s1.GetLength()) 
  168.         m_pFindDialog->m_szText = s1;
  169.  
  170.     if(m_pFindDialog->DoModal() != IDOK)
  171.     {
  172.         m_pFindDialog->m_szText = s2; // restore old string.  user selected cancel
  173.         return;
  174.     }
  175.     
  176.       ASSERT(m_pFindDialog->m_szText.GetLength());
  177.  
  178.     // add it to the combo control on the toolbar
  179.     comboBox.SetWindowText(m_pFindDialog->m_szText);
  180.     m_searchHistory.AddString(m_pFindDialog->m_szText);
  181.  
  182.     if(m_pFindDialog->m_szText.GetLength())
  183.         OnFindNext(m_pFindDialog->m_szText, m_pFindDialog->m_nDirection, m_pFindDialog->m_bMatchCase);
  184. }
  185.  
  186. void CNotepadView::OnUpdateEditFind(CCmdUI* pCmdUI) 
  187. {
  188.     pCmdUI->Enable(GetWindowTextLength());
  189. }
  190.  
  191. void CNotepadView::OnViewFindNext(CString &s1)
  192. {
  193.     OnFindNext(s1, m_pFindDialog->m_nDirection, m_pFindDialog->m_bMatchCase);
  194. }
  195.  
  196. void CNotepadView::OnViewFindNext() 
  197. {
  198.     CString s1;
  199.  
  200.     // if the user presses F3, see if there is a search pattern. if not, ask for one
  201.     if (m_pFindDialog->m_szText.GetLength())
  202.         OnFindNext(m_pFindDialog->m_szText, m_pFindDialog->m_nDirection, m_pFindDialog->m_bMatchCase);
  203.     else 
  204.         PostMessage(WM_COMMAND, ID_EDIT_FIND);
  205. }
  206.  
  207. BOOL CNotepadView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) 
  208. {
  209.     // change the view's font to SYSTEM_FIXED_FONT
  210.     CFont *font = CFont::FromHandle((HFONT)::GetStockObject(SYSTEM_FIXED_FONT));
  211.     BOOL bResult = CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
  212.     SetFont(font);
  213.  
  214.     return bResult;
  215. }
  216.  
  217. void CNotepadView::OnEditPaste() 
  218. {
  219.     CWnd* pComboWnd;
  220.     CWnd* pFocusWnd;
  221.     CComboBox& comboBox = ((CMainFrame*)AfxGetMainWnd())->m_wndToolBar.m_toolBarCombo;
  222.  
  223.     // Note: this is the handler for the accelerator (CTRL+V).  Since the accelerator goes to the
  224.     // frame, find who has the focus to determine where to paste to.
  225.  
  226.     pFocusWnd = GetFocus();
  227.     pComboWnd = comboBox.GetDlgItem(1001);
  228.  
  229.     ASSERT(pFocusWnd);
  230.     ASSERT(pComboWnd);
  231.  
  232.     // if the focus is in the combobox edit control, do the paste in the control
  233.     if(pFocusWnd->m_hWnd == pComboWnd->m_hWnd) 
  234.         comboBox.Paste();
  235.     else
  236.         GetEditCtrl().Paste();
  237. }
  238.  
  239. void CNotepadView::OnEditCopy() 
  240. {
  241.     CWnd* pComboWnd;
  242.     CWnd* pFocusWnd;
  243.     CComboBox& comboBox = ((CMainFrame*)AfxGetMainWnd())->m_wndToolBar.m_toolBarCombo;
  244.  
  245.     // Note: this is the handler for the accelerator (CTRL+C).  Since the accelerator goes to the
  246.     // frame, find who has the focus to determine where to copy from.
  247.  
  248.     pFocusWnd = GetFocus();
  249.     pComboWnd = comboBox.GetDlgItem(1001);
  250.  
  251.     ASSERT(pFocusWnd);
  252.     ASSERT(pComboWnd);
  253.  
  254.     // if the focus is in the combobox edit control, do the copy from the control
  255.     if(pFocusWnd->m_hWnd == pComboWnd->m_hWnd)
  256.         comboBox.Copy();
  257.     else
  258.         GetEditCtrl().Copy();
  259. }
  260.  
  261.